<?php
/**
 * Index.php
 * Author: luoji (867404364@qq.com)
 * Date: 2020-12-18
 * Time: 20:07
 */

namespace App\Http\Controllers\Api;


use App\Http\Controllers\Base;
use App\Tables;

class Index extends Base
{



    public function getNotifyList()
    {
        $list = Tables\Notify::where(['show' => Tables\Notify::SHOW])->orderBy('id','desc')->get();
        $return = [];
        foreach ($list as  $v) {
            $return[] = [
                'id' => $v['id'],
                'type' => $v['type'],
                'text' => $v['text'],
                'href' => $v['href'],
            ];
        }
        $list = Tables\DateSchedule::where('date', date('Y-m-d'))->where('status', 2)->get();
        foreach($list as $v) {
            $return[] = [
                'id' => $v['id'],
                'type' => $v['type']==Tables\DateSchedule::TYPE_Notify ? Tables\Notify::TYPE_Date_Notify : Tables\Notify::TYPE_Date_Important,
                'text' => $v['content'],
                'href' => '',
            ];
        }
        return $this->responseSuccess($return);
    }

    public function notifyDel()
    {
        $id = $this->request->post('id', 0);
        $type = $this->request->post('type', 0);
        $order = (bool) $this->request->post('order', false);
        if($id <= 0) {
            return $this->responseFail();
        }
        if(in_array($type, Tables\Notify::DATE_Notify_Arr)) {
            $d = Tables\DateSchedule::find($id);
            $d['status'] = 1;
            $ok = (bool) $d->save();
        } else {
            $query = [
                'show' => Tables\Notify::NO_SHOW
            ];
            if($order) {
                $query['type'] = Tables\Notify::TYPE_Order;
            }
            $ok = (bool) Tables\Notify::where(['id' => $id])->update($query);
        }

        return $ok ? $this->responseSuccess() : $this->responseFail();
    }

    public function getTurnover()
    {
        $todayPrice = Tables\Order::query()
            ->whereBetween('add_time', [strtotime(date('Y-m-d')), strtotime(date('Y-m-d')) + 86400])
            ->where('dumped', Tables\Order::No_Dumped)
            ->sum('real_price');
        $yt = strtotime(date('Y-m-d', strtotime('-1 day')));
        $yestdayPrice = Tables\Order::query()
            ->whereBetween('add_time', [$yt, $yt + 86400])
            ->where('dumped', Tables\Order::No_Dumped)
            ->sum('real_price');
        $noMake = Tables\OrderDetail::query()
                ->where('make_status', Tables\Order::Make_Status_No)
                ->where('outsource_id', 0)
                ->count();
        $noDeliver = Tables\Order::query()
            ->where('delivery_status', Tables\Order::Delivery_Status_No)
            ->where('dumped', Tables\Order::No_Dumped)
            ->count();
        $outsourceNoMake = Tables\OrderDetail::query()
                ->where('outsource_id', '>', 0)
                ->where('make_status', Tables\Order::Make_Status_No)
                ->count();
        $todayNoTake = Tables\OrderDetail::query()
            ->where('outsource_id', '>', 0)
            ->where('make_status', Tables\Order::Make_Status_Ing)
            ->where('complete_date', date('Y-m-d'))
            ->count();

        return $this->responseSuccess([
            'today' => $todayPrice,
            'yesterday' => $yestdayPrice,
            'noMake' => $noMake,
            'noDeliver' => $noDeliver,
            'outsourceNoMake' => $outsourceNoMake,
            'todayNoTake' => $todayNoTake
        ]);
    }

    public function addExpend()
    {
        $type = (int) $this->request->post('type');
        $content = $this->request->post('content');
        $money = (double) $this->request->post('money');
        if($type <= 0 and !in_array($type, [Tables\CashFlow::TYPE_Outcome_Common, Tables\CashFlow::TYPE_Outcome_Other])) {
            return $this->responseFail('类型错误');
        }
        if(empty($content)) {
            return $this->responseFail('请填写支出内容');
        }
        if($money <= 0) {
            return $this->responseFail('请填写支出金额');
        }

        $ok = (bool) Tables\CashFlow::insert([
            'change' => -$money,
            'desc'   => $content,
            'type'   => $type,
            'add_time' => time()
        ]);
        return $ok ? $this->responseSuccess() : $this->responseFail('操作失败！');
    }
}